﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComTools
{
    public partial class frmMsSqlSet : Form
    {
        public frmMsSqlSet()
        {
            InitializeComponent();
        }

		private string ConnKeyName
		{
			get
			{
				if (cbxKeyNames.SelectedIndex == -1)
				{
					cbxKeyNames.SelectedIndex = 0;
				}
				return cbxKeyNames.SelectedItem.ToString();
			}
		}

		private void btnOpenDBSet_Click(object sender, EventArgs e)
		{
			var builder = SqlSet();
			if (builder == null) { return; }
			System.Configuration.Configuration config =
				System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
			//
			var r = config.ConnectionStrings.ConnectionStrings[ConnKeyName];
			if (r == null)
			{
				config.ConnectionStrings.ConnectionStrings.Add(
					new System.Configuration.ConnectionStringSettings()
					{
						ConnectionString = builder.ConnectionString,
						Name = ConnKeyName
					});
			}
			else
			{
				r.ConnectionString = builder.ConnectionString;
			}
			//
			config.Save(System.Configuration.ConfigurationSaveMode.Modified);
			//
			System.Configuration.ConfigurationManager.RefreshSection("connectionStrings");
			LoadConn();
		}

        private void frmMsSqlSet_Load(object sender, EventArgs e)
        {
			LoadConn();
		}

		private void LoadConn()
        {
			var r  = System.Configuration.ConfigurationManager.ConnectionStrings[ConnKeyName];
			if (r != null)
            {
				var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(r.ConnectionString);

				txtDBSource.Text = builder.DataSource;
				txtDBName.Text = builder.InitialCatalog;
				txtUserName.Text = builder.UserID;
				txtPwd.Text = builder.Password;

				checkBox1.Checked = builder.IntegratedSecurity;

				txtDBConnStr.Text = builder.ConnectionString;
			}
		}

        private System.Data.SqlClient.SqlConnectionStringBuilder SqlSet()
        {
			Microsoft.Data.ConnectionUI.DataConnectionDialog dialog = new Microsoft.Data.ConnectionUI.DataConnectionDialog();
			//添加数据源
			//dialog.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.AccessDataSource);
			//dialog.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.OdbcDataSource);
			//dialog.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.OracleDataSource);
			dialog.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource);
			//dialog.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlFileDataSource);
			//设置默认数据提供对象
			dialog.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.SqlDataSource;
			dialog.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider;
			dialog.Text = "连接数据库";
			//只能够通过DataConnectionDialog类的静态方琺Show出对话框
			//不同使用dialog.Show()或dialog.ShowDialog()来呈现对话框
			if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dialog) == System.Windows.Forms.DialogResult.OK)
			{
				//连接字符串"Data Source=LENOVO-PC\\MYSQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True"

				var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(dialog.ConnectionString);
				//
				return builder;
            }
            else
            {
				return null;
            }
		}
		string appSetSqlKey = "SqlServerNames";
        private void btnSaveSqlKey_Click(object sender, EventArgs e)
        {
			var keyName = textBox1.Text.Trim();
			var r = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
			var app = r.AppSettings.Settings[appSetSqlKey];
			if (app == null)
            {
				r.AppSettings.Settings.Add(appSetSqlKey, keyName);
            } else
            {

            }
        }

        private void cbxKeyNames_SelectedIndexChanged(object sender, EventArgs e)
        {
			LoadConn();
        }
    }
}
